package org.example.model;
import org.apache.poi.xwpf.usermodel.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.ChartUtilities;
import static org.example.domain.ListTable.*;
import static org.example.domain.listforxml.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.poi.util.IOUtils;
import org.jfree.data.general.DefaultPieDataset;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class xmlinsertpic {
    public static void insert() {
        // 创建一个数据集

        //
        try {
            // 加载现有的Word文档
            XWPFDocument document = new XWPFDocument(new FileInputStream("output.docx"));
            boolean pie=false;
            boolean bar=false;
            boolean line=false;

            for (XWPFParagraph paragraph : document.getParagraphs()) {

                String text = paragraph.getText();
                Pattern pattern1 = Pattern.compile("<out var=pic xaixs=(\\w+) yaixs=(\\w+) bar>");
                Matcher matcher1 = pattern1.matcher(text);
                Pattern pattern2 = Pattern.compile("<out var=pic xaixs=(\\w+) yaixs=(\\w+) pie>");
                Matcher matcher2 = pattern2.matcher(text);
                Pattern pattern3 = Pattern.compile("<out var=pic xaixs=(\\w+) yaixs=(\\w+) line>");
                Matcher matcher3 = pattern3.matcher(text);
                if(matcher1.find()){
                    bar=true;
                    String xAxisKey = matcher1.group(1);
                    String yAxisKey = matcher1.group(2);
                    System.out.println(xAxisKey);
                    System.out.println(yAxisKey);

                    List<String> xAxisData = xmlhashTable.get(xAxisKey);
                    List<Double> yAxisData = xmlhashTable.get(yAxisKey).stream()
                            .map(Double::parseDouble) // 将每个字符串转换为double
                            .collect(Collectors.toList());

                    DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
                    for (int i = 0; i < xAxisData.size(); i++) {
                        dataset1.addValue(yAxisData.get(i), "Category", xAxisData.get(i));
                    }
                    JFreeChart chart1 = ChartFactory.createBarChart(
                            "Bar Chart", // chart title
                            "X-Axis", // domain axis label
                            "Y-Axis", // range axis label
                            dataset1, // data
                            PlotOrientation.VERTICAL,
                            false, // include legend
                            true,
                            false
                    );

                    ChartUtilities.saveChartAsPNG(new File("barchart.png"), chart1, 800, 600);



                }
                if(matcher2.find()) {
                    pie=true;
                    String xAxisKey = matcher2.group(1);
                    String yAxisKey = matcher2.group(2);
                    System.out.println(xAxisKey);
                    System.out.println(yAxisKey);
                    List<String> xAxisData = xmlhashTable.get(xAxisKey);
                    List<Double> yAxisData = xmlhashTable.get(yAxisKey).stream()
                            .map(Double::parseDouble) // 将每个字符串转换为double
                            .collect(Collectors.toList());
                    DefaultPieDataset dataset1 = new DefaultPieDataset();
                    for (int i = 0; i < xAxisData.size(); i++) {
                        dataset1.setValue(xAxisData.get(i),yAxisData.get(i));
                    }
                    JFreeChart pieChart = ChartFactory.createPieChart(
                            "Pie Chart", // chart title
                            dataset1, // data
                            false, // include legend
                            true,
                            false
                    );

                    // 保存饼图到文件
                    try {
                        ChartUtilities.saveChartAsPNG(new File("piechart.png"), pieChart, 800, 600);
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("Error saving the pie as a PNG file.");
                    }


                }
                if(matcher3.find()){
                    line=true;
                    String xAxisKey = matcher3.group(1);
                    String yAxisKey = matcher3.group(2);
                    System.out.println(xAxisKey);
                    System.out.println(yAxisKey);

                    List<String> xAxisData = xmlhashTable.get(xAxisKey);
                    List<Double> yAxisData = xmlhashTable.get(yAxisKey).stream()
                            .map(Double::parseDouble) // 将每个字符串转换为double
                            .collect(Collectors.toList());

                    DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
                    for (int i = 0; i < xAxisData.size(); i++) {
                        dataset1.addValue(yAxisData.get(i), "Category", xAxisData.get(i));
                    }
                    JFreeChart lineChart = ChartFactory.createLineChart(
                            "Line Chart Example", // 图表标题
                            "X-Axis (String)", // X轴标签，这里虽然是数值，但可以视为字符串
                            "Y-Axis (Double)", // Y轴标签
                            dataset1, // 数据集
                            PlotOrientation.VERTICAL,
                            true, // 是否显示图例
                            true,
                            false
                    );

                    // 保存折线图到文件
                    try {
                        File file = new File("LineChart.png");
                        ChartUtilities.saveChartAsPNG(file, lineChart, 800, 600);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }

            }
            if(pie==true){
                XWPFParagraph paragraph1 = document.createParagraph();

                // 在段落中添加一个新运行，用于插入图片
                XWPFRun run = paragraph1.createRun();

                // 读取图片文件
                InputStream inputStream = new FileInputStream("piechart.png");


                XWPFPicture picture = run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG,"piechart.png", 4000000, 3000000);
                // 保存文档

            }
            if(bar==true){
                XWPFParagraph paragraph1 = document.createParagraph();

                // 在段落中添加一个新运行，用于插入图片
                XWPFRun run = paragraph1.createRun();

                // 读取图片文件
                InputStream inputStream = new FileInputStream("barchart.png");


                XWPFPicture picture = run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG,"barchart.png", 4000000, 3000000);
                // 保存文档

            }
            if(line==true){
                XWPFParagraph paragraph1 = document.createParagraph();

                // 在段落中添加一个新运行，用于插入图片
                XWPFRun run = paragraph1.createRun();

                // 读取图片文件
                InputStream inputStream = new FileInputStream("linechart.png");


                XWPFPicture picture = run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG,"linechart.png", 4000000, 3000000);
                // 保存文档

            }


            try (FileOutputStream out = new FileOutputStream("output.docx")) {
                document.write(out);
            }

            // 关闭文档和图片的输入流
            document.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
